#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class Friend
{
private:
	string id;
	string name;
public:
	Friend(const string &id, const string &name):id(id),name(name)
	{
	}
	string getid()const
	{
		return id ;
	}
	string getname()const
	{
		return name ;
	}
	~Friend()
	{

	}
};
class Group
{
private:
	string groupname;
	int groupid;
	vector<string>members;
	string grouptype;
public:
	Group(const string& groupname, const int& groupid,  const string& grouptype)
		:groupname(groupname), groupid(groupid), grouptype(grouptype)
	{
	}
	string getgroupname()const
	{
		return groupname;
	}
	int getgroupid()const
	{
		return groupid;
	}
	string getgrouptype()const
	{
		return grouptype;
	}
	vector<string>getmembers()const
	{
		return members;
	}
	void addmember(const string &member)
	{
		members.push_back(member);
	}
    void removemember(const string& member)
    {
        for (auto it = members.begin(); it != members.end(); ++it) 
        {
            if (*it == member) 
            {
				members.erase(it);
				break;
            }
        }
    }
};
class User 
{
private:
	string id;
	string name;
	string birthday;
	string age;
	string location;
	vector<Friend>friends;
	vector<Group>groups;
	vector<bool>grouptype_open;
public:
	User(const string& id, const string& name):id(id), name(name)
	{
		grouptype_open.resize(3, false);
	}
	string getid()const
	{
		return id;
	}
	string getname()const
	{
		return name;
	}
	void addfriend(const Friend& a)
	{
		friends.push_back(a);
	}
	void removefriend(const string& friend_id)
	{
		for (auto it = friends.begin() ;it != friends.end();it++)
		{
			if (it->getid() == friend_id)
			{
				friends.erase(it);
				break;
			}
		}
	}
	Friend* findfriend(const string& friendid)
	{
		for (auto it = friends.begin(); it != friends.end(); it++)
		{
			if (it->getid() == friendid)
			{
				return &(*it);
			}
		}
		return nullptr;
	}
	void addgroup(const Group& b)
	{
		groups.push_back(b);
	}
	void removegroup(const int& group_id)
	{
		for (auto it = groups.begin(); it != groups.end(); it++)
		{
			if (it->getgroupid() == group_id)
			{
				groups.erase(it);
				break;
			}
		}
	}
	Group* findgroup(const int& group_id)
	{
		for (auto it = groups.begin(); it != groups.end(); it++)
		{
			if (it->getgroupid() == group_id)
			{
				return &(*it);
			}
		}
		return nullptr;
	}
	void opengrouptype (int type_index)
	{
		if (type_index >= 0 && type_index < grouptype_open.size())
		{
			grouptype_open[type_index] = true;
		}
	}
	void closegrouptype(int type_index)
	{
		if (type_index >= 0 && type_index < grouptype_open.size())
		{
			grouptype_open[type_index] = false;
		}
	}
	bool isgrouptypeopen(int type_index)const
	{
		if (type_index >= 0 && type_index < grouptype_open.size())
		{
			return grouptype_open[type_index];
		}
		return false;
	}
	void savetofile(const string& filename)
	{
		ofstream file(filename);
		if (file.is_open())
		{
			file << id << "," << name << "," << birthday << "," << age << "," << location << endl;
			file << friends.size() << endl;
			for (const auto& a : friends)
			{
				file << a.getid() << "," << a.getname() << endl;
			}
			file << groups.size() << endl;
			for (const auto& b : groups)
			{
				file << b.getgroupid() << "," << b.getgroupname() << "," << b.getgrouptype() << endl;
				file << b.getmembers().size() << endl;
				for (const auto& member : b.getmembers())
				{
					file << member << endl;
				}
			}
		}
	}

};
